﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace Blip.UI.Controls
{
    /// <summary>
    /// Kontrolka agregująca i wyświetlająca kontrolki typu <see cref="UploadProgressBar"/>.
    /// </summary>
    /// <seealso cref="UploadProgressBar"/>
    public partial class UploadProgressPanel : SurfaceUserControl
    {
        private Storyboard start;

        private Storyboard stop;

        /// <summary>
        /// Konstruktor inicjalizujący panel pasków postępu.
        /// </summary>
        public UploadProgressPanel()
        {
            InitializeComponent();
            Visibility = Visibility.Hidden;
            start = FindResource("showPanel") as Storyboard;
            stop = FindResource("hidePanel") as Storyboard;
        }

        /// <summary>
        /// Dodaje pasek postępu do panelu.
        /// </summary>
        /// <param name="bar">Nowy pasek postępu</param>
        /// <returns>Obiekt, na rzecz którego wywoływana jest metoda</returns>
        /// <seealso cref="UploadProgressBar"/>
        public UploadProgressPanel AddBar(UploadProgressBar bar)
        {
            panel.Children.Add(bar);
            bar.Finished += new EventHandler(OnBarFinished);
            if (panel.Children.Count == 1)
                Show();
            return this;
        }        

        /// <summary>
        /// Usuwa pasek postępu z panelu.
        /// </summary>
        /// <param name="bar">Usuwany pasek postępu</param>
        /// <returns>Obiekt, na rzecz którego wywoływana jest metoda</returns>
        public UploadProgressPanel RemoveBar(UploadProgressBar bar)
        {
            panel.Children.Remove(bar);
            if (panel.Children.Count == 0)
                Hide();
            return this;
        }        

        /// <summary>
        /// Wyświetla panel.
        /// </summary>
        protected void Show()
        {
            Visibility = Visibility.Visible;
            start.Begin(this);
        }

        /// <summary>
        /// Ukrywa panel.
        /// </summary>
        protected void Hide()
        {
            if (Visibility == Visibility.Visible)
                stop.Begin(this);
        }

        private void HideCompleted(object sender, EventArgs e)
        {
            Visibility = Visibility.Hidden;
        }

        private void OnBarFinished(object sender, EventArgs e)
        {
            RemoveBar((UploadProgressBar)sender);
        }
    }
}
